//#ifdef HasLog

/*
 * Created on 05-Jul-2005 at 23:45:29.
 * 
 * Copyright (c) 2005 Robert Virkus / Enough Software
 * 
 * This file is part of J2ME Polish.
 * 
 * J2ME Polish is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 * 
 * J2ME Polish is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with J2ME Polish; if not,
 * write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 * 
 * Commercial licenses are also available, please refer to the accompanying LICENSE.txt or visit
 * http://www.j2mepolish.org for details.
 */
package cn.com.youyuan.common;

import java.io.PrintStream;
import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;

public class FileLogger implements Runnable
{
	private static final int LOG_ENTRY_SIZE = 20;
	private static final int STRING_BUFFER_SIZE = 100;

	private Vector m_entriesPool;
	private PrintStream m_printStream;
	private boolean m_errorFlag;
	private StringBuffer m_stringBuffer = new StringBuffer(STRING_BUFFER_SIZE);

	public void handleLogEntry(String entry)
	{
		if (m_errorFlag)
		{
			return;
		}

		if (m_entriesPool == null)
		{
			m_entriesPool = new Vector(LOG_ENTRY_SIZE);
			new Thread(this).start();
		}

		synchronized (m_entriesPool)
		{
			m_entriesPool.addElement(entry);
			m_entriesPool.notify();
		}
	}

	public void exit()
	{
		if (m_printStream != null)
		{
			m_printStream.close();
			m_printStream = null;
		}
	}

	public void run()
	{
		try
		{
			FileConnection connection = null;

			String url = null;
			String root = null;
			Enumeration enumeration = FileSystemRegistry.listRoots();

			while (enumeration.hasMoreElements())
			{
				root = (String) enumeration.nextElement();
			}
//			root += "c:/predefgallery/predeffilereceived/";
			root += "other/";
			url = "file:///" + root + "j2melog.txt";

			connection = (FileConnection) Connector.open(url, Connector.READ_WRITE);
			if (connection.exists())
			{
				connection.delete();
			}

			connection.create();

			m_printStream = new PrintStream(connection.openOutputStream());

			while (true)
			{
				while (m_entriesPool.size() != 0)
				{
					String entry;
					synchronized (m_entriesPool)
					{
						entry = (String) m_entriesPool.elementAt(0);
						m_entriesPool.removeElementAt(0);
					}

					m_stringBuffer.setLength(0);
					m_stringBuffer.append(entry);

					m_printStream.println(m_stringBuffer.toString());
					m_printStream.flush();
				}

				synchronized (m_entriesPool)
				{
					m_entriesPool.wait();
				}
			}
		}
		catch (Exception e)
		{

		}
	}
}
//#endif